package com.hlhj.funlike_business.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.adapter.CommentAdp;
import com.hlhj.funlike_business.bean.CommentListBean;
import com.hlhj.funlike_business.presenter.CommentPresenter;
import com.hlhj.funlike_business.views.CommentView;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class CommentAty extends BaseActivity<CommentView,CommentPresenter> implements CommentView {
    private RecyclerView recycle_list;
    private ImageView img_back;
    private SpringView spring;
    private TextView tv_title;
    private CommentAdp commentAdp;
    private RelativeLayout  ll_edit;
    private EditText edit_content;
    private TextView tv_send;
    private List<CommentListBean.DataBeanX.DataBean> dataBeanList;
    private int page=1;
    private int commentId;
    private String commentContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @Override
    protected int getContentId() {
        return R.layout.aty_commentlsit;
    }

    @Override
    protected void initData() {
        tv_title.setText("收到的评价");
        dataBeanList = new ArrayList<>();
        commentAdp = new CommentAdp(dataBeanList);
        recycle_list.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        recycle_list.setAdapter(commentAdp);

        spring.setHeader(new DefaultHeader(this));
        spring.setFooter(new DefaultFooter(this));
    }

    @NotNull
    @Override
    public CommentView bindView() {
        return this;
    }

    @NotNull
    @Override
    public CommentPresenter createPresenter() {
        return new CommentPresenter();
    }

    @Override
    public void initView() {
        tv_title=findViewById(R.id.tv_title);
        img_back=findViewById(R.id.img_back);
        recycle_list=findViewById(R.id.recycle_list);
        spring=findViewById(R.id.spring);
        ll_edit=findViewById(R.id.ll_edit);
        edit_content=findViewById(R.id.edit_content);
        tv_send=findViewById(R.id.tv_send);
    }

    @Override
    public void logicStart() {
        getPresenter().LoadCommentList(this,page);
        commentAdp.setListener(new CommentAdp.getResult() {
            @Override
            public void loadDel(int id) {
                getPresenter().LoadDelResult(CommentAty.this,id);
            }

            @Override
            public void loadReply(int id) {
                ll_edit.setVisibility(View.VISIBLE);
                commentId=id;
            }
        });
    }

    @Override
    public void setListener() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        spring.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page=1;
                getPresenter().LoadCommentList(CommentAty.this,page);
                spring.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {
                page++;
                getPresenter().LoadCommentList(CommentAty.this,page);
                spring.onFinishFreshAndLoad();
            }
        });

        edit_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                commentContent=charSequence.toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        tv_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getPresenter().LoadReplyResult(CommentAty.this,commentId,commentContent);
            }
        });
    }

    @Override
    public void showCommentList(List<CommentListBean.DataBeanX.DataBean> dataBeans) {
        if (page==1){
            dataBeanList.clear();
            dataBeanList.addAll(dataBeans);
        }else {
            dataBeanList.addAll(dataBeans);
        }
        commentAdp.notifyDataSetChanged();
    }

    @Override
    public void showResult(String msg) {
        Toast.makeText(CommentAty.this,msg.toString(),Toast.LENGTH_SHORT).show();
        ll_edit.setVisibility(View.GONE);
        edit_content.setText("");
        getPresenter().LoadCommentList(CommentAty.this,1);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            if (ll_edit.getVisibility()==View.VISIBLE){
                ll_edit.setVisibility(View.GONE);
                return true;
            }
            return false;
        }
        return super.onKeyLongPress(keyCode, event);
    }
}
